local skel = fk.CreateSkill {
  name = "efengqi__qixi",
}

Fk:loadTranslationTable{
  ["efengqi__qixi"] = "奇袭",
  [":efengqi__qixi"] = "你可以将黑色牌当【过河拆桥】使用，然后你可以交出一张红色牌，分配弃置牌。",

  ["#efengqi__qixi"] = "奇袭：将黑色牌当【过河拆桥】使用，然后可交出一张红色牌，分配弃置牌",
  ["#efengqi__qixi-red"] = "奇袭：你可以交出一张红色牌，分配 %arg 张弃置牌",
  ["#efengqi__qixi_delay"] = "奇袭",

  ["$efengqi__qixi1"] = "弟兄们，准备动手！",
  ["$efengqi__qixi2"] = "你用不了这么多了！",
}

skel:addEffect("viewas", {
  anim_type = "control",
  pattern = "dismantlement",
  prompt = "#efengqi__qixi",
  card_filter = function (self, player, to_select, selected)
    return #selected == 0 and Fk:getCardById(to_select).color == Card.Black
  end,
  view_as = function (self, player, cards)
    if #cards ~= 1 then return nil end
    local c = Fk:cloneCard("dismantlement")
    c.skillName = skel.name
    c:addSubcard(cards[1])
    return c
  end,
  enabled_at_response = function (self, player, response)
    return not response
  end
})

skel:addEffect(fk.CardUseFinished, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and not player:isNude()
    and table.contains(data.card.skillNames, skel.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local useEvent = room.logic:getCurrentEvent():findParent(GameEvent.UseCard, true)
    if not useEvent then return end
    local ids = {}
    useEvent:searchEvents(GameEvent.MoveCards, 1, function (e)
      if e.parent and e.parent.parent and e.parent.parent.event == GameEvent.CardEffect
      and e.parent.parent.data.card == data.card then
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile and move.moveReason == fk.ReasonDiscard and move.skillName:endsWith("dismantlement_skill") then
            for _, info in ipairs(move.moveInfo) do
              if room:getCardArea(info.cardId) == Card.DiscardPile then
                table.insertIfNeed(ids, info.cardId)
              end
            end
          end
        end
      end
    end)
    if #ids > 0 then
      local tos, cards = room:askToChooseCardsAndPlayers(player, {
        targets = room:getOtherPlayers(player, false), min_num = 1, max_num = 1,
        pattern = ".|.|diamond,heart", min_card_num = 1, max_card_num = 1, skill_name = skel.name,
        prompt = "#efengqi__qixi-red:::"..#ids,
      })
      if #tos == 0 or #cards == 0 then return end
      room:obtainCard(tos[1], cards, true, fk.ReasonGive, player, skel.name)
      if player.dead then return end
      ids = table.filter(ids, function (id) return room:getCardArea(id) == Card.DiscardPile end)
      if #ids == 0 then return end
      room:askToYiji(player, {
        cards = ids, skill_name = skel.name, min_num = #ids, max_num = #ids, targets = room.alive_players, expand_pile = ids,
      })
    end
  end,
})


return skel
